package com.zyd.blog.personal.controller;

import com.zyd.blog.business.annotation.BussinessLog;
import com.zyd.blog.business.enums.ResponseStatus;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.framework.object.ResponseVO;
import com.zyd.blog.personal.service.MerchantApplyDealService;
import com.zyd.blog.personal.vo.MerchantApplyInfoConditionVo;
import com.zyd.blog.util.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 合作商家信息列表
 * @author mfy
 */
@Controller
@RequestMapping("/merchantInfo")
public class MerchantInfoController {

    @Resource
    private MerchantApplyDealService merchantApplyDealService;

    /**
     * 进入合作商家信息列表
     * 访问路径 http://localhost:8085/merchantInfo/page
     * @return
     */
    @BussinessLog("进入合作商家信息列表界面")
    @GetMapping("/page")
    public ModelAndView front() {
        return ResultUtil.view("merchantInfo/list");
    }

    /**
     * 得到全部商家合作信息
     * 访问路径 http://localhost:8085/merchantInfo/list
     * @param merchantApplyInfoConditionVo  查询条件
     * @return  商家合作信息
     */
    @RequestMapping("/list")
    @BussinessLog("得到商家合作申请列表")
    @ResponseBody
    public PageResult list(MerchantApplyInfoConditionVo merchantApplyInfoConditionVo) {
        List<Map<String,Object>> merchantApplyInfo = merchantApplyDealService.getMerchantList(merchantApplyInfoConditionVo);
        return ResultUtil.tablePage((long) merchantApplyInfo.size(),merchantApplyInfo);
    }

    /**
     * 获取商家合作详细信息
     * 访问路径 http://localhost:8085/merchantInfo/detail/{id}
     * @param id    合作id
     * @return  详细信息
     */
    @RequestMapping("/detail/{id}")
    @ResponseBody
    public ResponseVO detail(@PathVariable("id") Long id) {
        return ResultUtil.success(null, merchantApplyDealService.getDetailMerchantApplyInfo(id));
    }

    /**
     * 查看结束
     * 访问路径 http://localhost:8085/merchantInfo/ok
     * @return  查看结束
     */
    @PostMapping("/ok")
    @BussinessLog("查看结束")
    @ResponseBody
    public ResponseVO ok() {
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    /**
     * 修改商家合作状态
     * 访问路径 http://localhost:8085/merchantInfo/change
     * @param status    状态
     * @param id    合作id
     * @return  操作结果
     */
    @RequestMapping("/change")
    @BussinessLog("编辑用户")
    @ResponseBody
    public ResponseVO change(String status, Long id) {
        try {
            merchantApplyDealService.changeStatus(id,status);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("状态修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    /**
     * 删除
     * 访问路径 http://localhost:8085/merchantInfo/del
     * @param id    合作id
     * @return  操作结果
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResponseVO del(Long id){
        try {
            merchantApplyDealService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

}
